{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90144630",
   "metadata": {},
   "source": [
    "————————————————————————\n",
    "\n",
    "* 本学习记录人：林涵萩\n",
    "* 学习时间：2021-11-02\n",
    "* week10\n",
    "* 本记录主要参考文档：\n",
    "> 1. [python官方文档](https://www.python.org/)\n",
    ">> 1. [Tutorial](https://docs.python.org/3/tutorial/index.html)\n",
    ">> 2. [Library Reference](https://docs.python.org/3/library/index.html)\n",
    "> 2. 徐老师博客：[博客文档](https://www.cnblogs.com/xuzhichao/p/11407192.html)\n",
    "\n",
    "——————————————————————————"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d7470aa",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "\n",
    "## 1. 字符串和数值的一般方法\n",
    "\n",
    "### 1. 字符串\n",
    "\n",
    "> 1. [字符串概念](https://docs.python.org/3/tutorial/introduction.html#strings)\n",
    ">> 1. single quotes('...')\n",
    ">> 2. double quotes('...')\n",
    ">> 3. <font style=\"color:red\">\"\"\"...\"\"\"or'''...'''</font>：不改变文本样式\n",
    ">> 4. 字符串的拼接：strl+str2,连接变量或变量和文字，请使用+\n",
    ">> 5. 字符串的索引性质（**index**）：字符串可以被索引（下标），第一个字符的索引为0\n",
    ">>> * 案例\n",
    "```\n",
    "+---+---+---+---+---+---+\n",
    "| P | y | t | h | o | n |\n",
    "+---+---+---+---+---+---+\n",
    "0   1   2   3   4   5   6\n",
    "-6  -5  -4  -3  -2  -1\n",
    "\n",
    "```\n",
    ">> 6. 字符串的切片(**slice**) : [start:stop]\n",
    "\n",
    "> 2. [字符串的一般方法-String Methods](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)\n",
    "\n",
    "> * index从0开始    slice左闭右开"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dabe151e",
   "metadata": {},
   "source": [
    " #### 1.1 字符串的一般方法\n",
    "\n",
    " * 应掌握：\n",
    " > 1. 知道str的方法使用一般操作 str.methods_name()\n",
    " > 2. 了解python的字符串官方网站查询位置（或者能够在搜索浏览器查询）\n",
    " > 3. 能够在后续遇到字符串问题时能找到解决方法\n",
    " > 4. 了解常见方法：count,capitalize,isdigit,format,strip,split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a8dbc3de",
   "metadata": {},
   "outputs": [],
   "source": [
    "text =\"python\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "15086e64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str.capitalize\n",
    "# 返回字符串副本，其中第一个字符大写，其余字符小写\n",
    "\"python\".capitalize()\n",
    "text.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "01fc0e73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'     python    '"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str.center(width) width是一个数值\n",
    "text.center(15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a5aeda3",
   "metadata": {},
   "source": [
    "* 只有当width足够大时才会有明显效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "80612aaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str.count()\n",
    "text_center = \"Return centered in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).\"\n",
    "text_center.count(\"a\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebd024e9",
   "metadata": {},
   "source": [
    "* 计算某个文本在文段中的出现次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d379790f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str.isdigit() 判断字符串是否是数值     False 不是数值 Ture 是数值\n",
    "text_center.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "faeab1f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id = \"      21302000001      \"\n",
    "stu_id.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a5521ebc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'21302000001'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str.strip 清除文本中首位和末位的所有空格\n",
    "stu_id.strip()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "015a8dde",
   "metadata": {},
   "source": [
    "### 2. 数值\n",
    "> 1. python作为运算器，了解一般的运算符\n",
    "> 2. 了解运算符的先后顺序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cf6f09c",
   "metadata": {},
   "source": [
    "## 2. [for循环遍历](https://docs.python.org/3/tutorial/controlflow.html#for-statements)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50d858a6",
   "metadata": {},
   "source": [
    "#### 1. for循环+range()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "929b4ba8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 1. for 循环\n",
    "# 2. range()\n",
    "# 总结\n",
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0939c293",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "for ch in \"python\":\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ed03ec82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "r\n",
      "e\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "for ch in \"red\":\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5161156f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n",
      "H\n"
     ]
    }
   ],
   "source": [
    "for ch in \"pH\":\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5bfd638",
   "metadata": {},
   "source": [
    "* ch代表引号内字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "363f6b3c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n"
     ]
    }
   ],
   "source": [
    "# range与for循环使用，主要目标：决定for循环执行次数\n",
    "# i 指 item内容，item是一个变量，每次循环都会变\n",
    "# 第一次循环 i = 0，第二次循环i = 1\n",
    "for num in range(5):\n",
    "    # 如果没有用到 num 这个值\n",
    "    print('Head First Rocks!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bf61a03d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for num in range(5):\n",
    "    print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "67ec366b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "A\n",
      "A\n",
      "A\n",
      "A\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "423f6bd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "k\n",
      "k\n",
      "k\n",
      "k\n",
      "k\n"
     ]
    }
   ],
   "source": [
    "for p in range(5):\n",
    "    print('k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "98860f75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n",
      "OHHHHHHHHHH!!!\n"
     ]
    }
   ],
   "source": [
    "for num in range(10):\n",
    "    print(\"OHHHHHHHHHH!!!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c39f0fb",
   "metadata": {},
   "source": [
    "* i可以替换为其他项目，range()决定print()执行次数。print(i)执行range()范围内的数字循环（从0开始），print('文本')执行range()范围内的重复次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4eb1cf8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/xxyw/index.htm\n",
      "https://www.nfu.edu.cn/xxyw/index1.htm\n",
      "https://www.nfu.edu.cn/xxyw/index2.htm\n",
      "https://www.nfu.edu.cn/xxyw/index3.htm\n",
      "https://www.nfu.edu.cn/xxyw/index4.htm\n",
      "https://www.nfu.edu.cn/xxyw/index5.htm\n",
      "https://www.nfu.edu.cn/xxyw/index6.htm\n",
      "https://www.nfu.edu.cn/xxyw/index7.htm\n",
      "https://www.nfu.edu.cn/xxyw/index8.htm\n",
      "https://www.nfu.edu.cn/xxyw/index9.htm\n",
      "https://www.nfu.edu.cn/xxyw/index10.htm\n",
      "https://www.nfu.edu.cn/xxyw/index11.htm\n",
      "https://www.nfu.edu.cn/xxyw/index12.htm\n",
      "https://www.nfu.edu.cn/xxyw/index13.htm\n",
      "https://www.nfu.edu.cn/xxyw/index14.htm\n",
      "https://www.nfu.edu.cn/xxyw/index15.htm\n",
      "https://www.nfu.edu.cn/xxyw/index16.htm\n",
      "https://www.nfu.edu.cn/xxyw/index17.htm\n",
      "https://www.nfu.edu.cn/xxyw/index18.htm\n",
      "https://www.nfu.edu.cn/xxyw/index19.htm\n",
      "https://www.nfu.edu.cn/xxyw/index20.htm\n",
      "https://www.nfu.edu.cn/xxyw/index21.htm\n",
      "https://www.nfu.edu.cn/xxyw/index22.htm\n",
      "https://www.nfu.edu.cn/xxyw/index23.htm\n",
      "https://www.nfu.edu.cn/xxyw/index24.htm\n",
      "https://www.nfu.edu.cn/xxyw/index25.htm\n",
      "https://www.nfu.edu.cn/xxyw/index26.htm\n",
      "https://www.nfu.edu.cn/xxyw/index27.htm\n",
      "https://www.nfu.edu.cn/xxyw/index28.htm\n",
      "https://www.nfu.edu.cn/xxyw/index29.htm\n",
      "https://www.nfu.edu.cn/xxyw/index30.htm\n",
      "https://www.nfu.edu.cn/xxyw/index31.htm\n",
      "https://www.nfu.edu.cn/xxyw/index32.htm\n",
      "https://www.nfu.edu.cn/xxyw/index33.htm\n",
      "https://www.nfu.edu.cn/xxyw/index34.htm\n",
      "https://www.nfu.edu.cn/xxyw/index35.htm\n",
      "https://www.nfu.edu.cn/xxyw/index36.htm\n",
      "https://www.nfu.edu.cn/xxyw/index37.htm\n",
      "https://www.nfu.edu.cn/xxyw/index38.htm\n",
      "https://www.nfu.edu.cn/xxyw/index39.htm\n",
      "https://www.nfu.edu.cn/xxyw/index40.htm\n",
      "https://www.nfu.edu.cn/xxyw/index41.htm\n",
      "https://www.nfu.edu.cn/xxyw/index42.htm\n",
      "https://www.nfu.edu.cn/xxyw/index43.htm\n",
      "https://www.nfu.edu.cn/xxyw/index44.htm\n",
      "https://www.nfu.edu.cn/xxyw/index45.htm\n",
      "https://www.nfu.edu.cn/xxyw/index46.htm\n",
      "https://www.nfu.edu.cn/xxyw/index47.htm\n",
      "https://www.nfu.edu.cn/xxyw/index48.htm\n",
      "https://www.nfu.edu.cn/xxyw/index49.htm\n",
      "https://www.nfu.edu.cn/xxyw/index50.htm\n",
      "https://www.nfu.edu.cn/xxyw/index51.htm\n",
      "https://www.nfu.edu.cn/xxyw/index52.htm\n",
      "https://www.nfu.edu.cn/xxyw/index53.htm\n",
      "https://www.nfu.edu.cn/xxyw/index54.htm\n",
      "https://www.nfu.edu.cn/xxyw/index55.htm\n",
      "https://www.nfu.edu.cn/xxyw/index56.htm\n",
      "https://www.nfu.edu.cn/xxyw/index57.htm\n",
      "https://www.nfu.edu.cn/xxyw/index58.htm\n",
      "https://www.nfu.edu.cn/xxyw/index59.htm\n",
      "https://www.nfu.edu.cn/xxyw/index60.htm\n",
      "https://www.nfu.edu.cn/xxyw/index61.htm\n",
      "https://www.nfu.edu.cn/xxyw/index62.htm\n",
      "https://www.nfu.edu.cn/xxyw/index63.htm\n",
      "https://www.nfu.edu.cn/xxyw/index64.htm\n",
      "https://www.nfu.edu.cn/xxyw/index65.htm\n",
      "https://www.nfu.edu.cn/xxyw/index66.htm\n",
      "https://www.nfu.edu.cn/xxyw/index67.htm\n",
      "https://www.nfu.edu.cn/xxyw/index68.htm\n",
      "https://www.nfu.edu.cn/xxyw/index69.htm\n",
      "https://www.nfu.edu.cn/xxyw/index70.htm\n",
      "https://www.nfu.edu.cn/xxyw/index71.htm\n",
      "https://www.nfu.edu.cn/xxyw/index72.htm\n",
      "https://www.nfu.edu.cn/xxyw/index73.htm\n",
      "https://www.nfu.edu.cn/xxyw/index74.htm\n",
      "https://www.nfu.edu.cn/xxyw/index75.htm\n",
      "https://www.nfu.edu.cn/xxyw/index76.htm\n",
      "https://www.nfu.edu.cn/xxyw/index77.htm\n",
      "https://www.nfu.edu.cn/xxyw/index78.htm\n",
      "https://www.nfu.edu.cn/xxyw/index79.htm\n",
      "https://www.nfu.edu.cn/xxyw/index80.htm\n",
      "https://www.nfu.edu.cn/xxyw/index81.htm\n",
      "https://www.nfu.edu.cn/xxyw/index82.htm\n",
      "https://www.nfu.edu.cn/xxyw/index83.htm\n",
      "https://www.nfu.edu.cn/xxyw/index84.htm\n",
      "https://www.nfu.edu.cn/xxyw/index85.htm\n",
      "https://www.nfu.edu.cn/xxyw/index86.htm\n",
      "https://www.nfu.edu.cn/xxyw/index87.htm\n",
      "https://www.nfu.edu.cn/xxyw/index88.htm\n",
      "https://www.nfu.edu.cn/xxyw/index89.htm\n",
      "https://www.nfu.edu.cn/xxyw/index90.htm\n",
      "https://www.nfu.edu.cn/xxyw/index91.htm\n",
      "https://www.nfu.edu.cn/xxyw/index92.htm\n",
      "https://www.nfu.edu.cn/xxyw/index93.htm\n",
      "https://www.nfu.edu.cn/xxyw/index94.htm\n",
      "https://www.nfu.edu.cn/xxyw/index95.htm\n"
     ]
    }
   ],
   "source": [
    "# 实践：抓取学校所有新闻页面链接\n",
    "# 用到的知识点：1.变量、赋值 2.字符串拼接 3.循环\n",
    "# 南方学院 学校要闻\n",
    "学校域名 = \"https://www.nfu.edu.cn/\"\n",
    "学校要闻 = \"xxyw/\"\n",
    "学校要闻第一页 = 学校域名 + 学校要闻 + \"index\" + \"0\" + \".htm\"\n",
    "for i in range(96):\n",
    "    if i == 0:\n",
    "        print(学校域名 + 学校要闻 + \"index\" +  \".htm\")\n",
    "    else:\n",
    "        print(学校域名 + 学校要闻 + \"index\" + str(i) + \".htm\")\n",
    "# 其他：如 校园动态 等，更改两个内容，xxdt和页数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3595c361",
   "metadata": {},
   "source": [
    "> 1. range()可以生成一组序列数值\n",
    ">> * 如：range(10)，生成十个数，从0开始，每次+1\n",
    ">> * 如：range(5，10),生成stop-start个数 即10-5=5 个数，从5开始，每次+1\n",
    ">> * 如：range(0,10,3)，10-0=10个数， 从0开始， 每次+3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7aa8e5f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# range：范围，10 代表：1.10个数值，2.终止点是10\n",
    "# 只有一个值：start：0（默认）       stop（终止值）：10  step（步长）：1\n",
    "# range()可以生成一组序列数值，10，生成10个数，从0开始，每次 +1\n",
    "list(range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "64918e6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只有两个值：start：5   stop（终止值）：10  step（步长）：1（默认）\n",
    "list(range(5,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ae947dc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 有三个值：start：0   stop（终止值）：10  step（步长）：2\n",
    "list(range(0,10,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d8801e9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,9,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb16120d",
   "metadata": {},
   "source": [
    "## 2. random模块（随机数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "5072c435",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "648b4fcc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6978827797354679"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# random() 随机取值\n",
    "random.random()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "633e831d",
   "metadata": {},
   "source": [
    "* 随机取值没有限定数字类型，不会为整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "15278cd0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "62\n",
      "74\n",
      "19\n",
      "95\n",
      "23\n",
      "88\n",
      "97\n",
      "85\n"
     ]
    }
   ],
   "source": [
    "# 福利彩票生成器\n",
    "本次开奖 = \"7,16,19,25,26,32,14,5\"\n",
    "for i in range(8):\n",
    "    print(round(random.random()*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "031ed5ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(random.random()*30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "3d83aea5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(random.random()*3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca895bf9",
   "metadata": {},
   "source": [
    "* round()保证取值为整数，random.random()* x（x为未知数）限定取值范围，**默认从0开始**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "1bd18e87",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给定数值范围\n",
    "random.randint(1,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "0b8eaa2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 摇骰子游戏\n",
    "for i in range(2):\n",
    "    print(random.randint(1,6))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c91c1b26",
   "metadata": {},
   "source": [
    "* random.randint()和random.random()* x的区别在于random.randint()给的是范围，而random.random()* x只给出了数值上限"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "471bf82c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# choice 随机点名（且只点一次）\n",
    "stu_names = [\"A\",\"B\",\"C\",\"D\"]\n",
    "random.choice(stu_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "7507be87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A,B,C,DA,B,C,D'"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_names = [\"A,B,C,D\"]\n",
    "random.choice(stu_names)*2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e50d430c",
   "metadata": {},
   "source": [
    "* 被选择的文本必须要是一个一个的独立文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "0d5995a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# help(me!!!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "f87916b4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "randint(a, b) method of Random instance\n",
      "        Return random integer in range [a, b], including both end points.\n",
      "    \n",
      "    random() method of Random instance\n",
      "        random() -> x in the interval [0, 1).\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('''\n",
    "randint(a, b) method of Random instance\n",
    "        Return random integer in range [a, b], including both end points.\n",
    "    \n",
    "    random() method of Random instance\n",
    "        random() -> x in the interval [0, 1).\n",
    "\n",
    "''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "a1820e4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module random:\n",
      "\n",
      "NAME\n",
      "    random - Random variable generators.\n",
      "\n",
      "MODULE REFERENCE\n",
      "    https://docs.python.org/3.8/library/random\n",
      "    \n",
      "    The following documentation is automatically generated from the Python\n",
      "    source files.  It may be incomplete, incorrect or include features that\n",
      "    are considered implementation detail and may vary between Python\n",
      "    implementations.  When in doubt, consult the module reference at the\n",
      "    location listed above.\n",
      "\n",
      "DESCRIPTION\n",
      "        integers\n",
      "        --------\n",
      "               uniform within range\n",
      "    \n",
      "        sequences\n",
      "        ---------\n",
      "               pick random element\n",
      "               pick random sample\n",
      "               pick weighted random sample\n",
      "               generate random permutation\n",
      "    \n",
      "        distributions on the real line:\n",
      "        ------------------------------\n",
      "               uniform\n",
      "               triangular\n",
      "               normal (Gaussian)\n",
      "               lognormal\n",
      "               negative exponential\n",
      "               gamma\n",
      "               beta\n",
      "               pareto\n",
      "               Weibull\n",
      "    \n",
      "        distributions on the circle (angles 0 to 2pi)\n",
      "        ---------------------------------------------\n",
      "               circular uniform\n",
      "               von Mises\n",
      "    \n",
      "    General notes on the underlying Mersenne Twister core generator:\n",
      "    \n",
      "    * The period is 2**19937-1.\n",
      "    * It is one of the most extensively tested generators in existence.\n",
      "    * The random() method is implemented in C, executes in a single Python step,\n",
      "      and is, therefore, threadsafe.\n",
      "\n",
      "CLASSES\n",
      "    _random.Random(builtins.object)\n",
      "        Random\n",
      "            SystemRandom\n",
      "    \n",
      "    class Random(_random.Random)\n",
      "     |  Random(x=None)\n",
      "     |  \n",
      "     |  Random number generator base class used by bound module functions.\n",
      "     |  \n",
      "     |  Used to instantiate instances of Random to get generators that don't\n",
      "     |  share state.\n",
      "     |  \n",
      "     |  Class Random can also be subclassed if you want to use a different basic\n",
      "     |  generator of your own devising: in that case, override the following\n",
      "     |  methods:  random(), seed(), getstate(), and setstate().\n",
      "     |  Optionally, implement a getrandbits() method so that randrange()\n",
      "     |  can cover arbitrarily large ranges.\n",
      "     |  \n",
      "     |  Method resolution order:\n",
      "     |      Random\n",
      "     |      _random.Random\n",
      "     |      builtins.object\n",
      "     |  \n",
      "     |  Methods defined here:\n",
      "     |  \n",
      "     |  __getstate__(self)\n",
      "     |      # Issue 17489: Since __reduce__ was defined to fix #759889 this is no\n",
      "     |      # longer called; we leave it here because it has been here since random was\n",
      "     |      # rewritten back in 2001 and why risk breaking something.\n",
      "     |  \n",
      "     |  __init__(self, x=None)\n",
      "     |      Initialize an instance.\n",
      "     |      \n",
      "     |      Optional argument x controls seeding, as for Random.seed().\n",
      "     |  \n",
      "     |  __reduce__(self)\n",
      "     |      Helper for pickle.\n",
      "     |  \n",
      "     |  __setstate__(self, state)\n",
      "     |  \n",
      "     |  betavariate(self, alpha, beta)\n",
      "     |      Beta distribution.\n",
      "     |      \n",
      "     |      Conditions on the parameters are alpha > 0 and beta > 0.\n",
      "     |      Returned values range between 0 and 1.\n",
      "     |  \n",
      "     |  choice(self, seq)\n",
      "     |      Choose a random element from a non-empty sequence.\n",
      "     |  \n",
      "     |  choices(self, population, weights=None, *, cum_weights=None, k=1)\n",
      "     |      Return a k sized list of population elements chosen with replacement.\n",
      "     |      \n",
      "     |      If the relative weights or cumulative weights are not specified,\n",
      "     |      the selections are made with equal probability.\n",
      "     |  \n",
      "     |  expovariate(self, lambd)\n",
      "     |      Exponential distribution.\n",
      "     |      \n",
      "     |      lambd is 1.0 divided by the desired mean.  It should be\n",
      "     |      nonzero.  (The parameter would be called \"lambda\", but that is\n",
      "     |      a reserved word in Python.)  Returned values range from 0 to\n",
      "     |      positive infinity if lambd is positive, and from negative\n",
      "     |      infinity to 0 if lambd is negative.\n",
      "     |  \n",
      "     |  gammavariate(self, alpha, beta)\n",
      "     |      Gamma distribution.  Not the gamma function!\n",
      "     |      \n",
      "     |      Conditions on the parameters are alpha > 0 and beta > 0.\n",
      "     |      \n",
      "     |      The probability distribution function is:\n",
      "     |      \n",
      "     |                  x ** (alpha - 1) * math.exp(-x / beta)\n",
      "     |        pdf(x) =  --------------------------------------\n",
      "     |                    math.gamma(alpha) * beta ** alpha\n",
      "     |  \n",
      "     |  gauss(self, mu, sigma)\n",
      "     |      Gaussian distribution.\n",
      "     |      \n",
      "     |      mu is the mean, and sigma is the standard deviation.  This is\n",
      "     |      slightly faster than the normalvariate() function.\n",
      "     |      \n",
      "     |      Not thread-safe without a lock around calls.\n",
      "     |  \n",
      "     |  getstate(self)\n",
      "     |      Return internal state; can be passed to setstate() later.\n",
      "     |  \n",
      "     |  lognormvariate(self, mu, sigma)\n",
      "     |      Log normal distribution.\n",
      "     |      \n",
      "     |      If you take the natural logarithm of this distribution, you'll get a\n",
      "     |      normal distribution with mean mu and standard deviation sigma.\n",
      "     |      mu can have any value, and sigma must be greater than zero.\n",
      "     |  \n",
      "     |  normalvariate(self, mu, sigma)\n",
      "     |      Normal distribution.\n",
      "     |      \n",
      "     |      mu is the mean, and sigma is the standard deviation.\n",
      "     |  \n",
      "     |  paretovariate(self, alpha)\n",
      "     |      Pareto distribution.  alpha is the shape parameter.\n",
      "     |  \n",
      "     |  randint(self, a, b)\n",
      "     |      Return random integer in range [a, b], including both end points.\n",
      "     |  \n",
      "     |  randrange(self, start, stop=None, step=1, _int=<class 'int'>)\n",
      "     |      Choose a random item from range(start, stop[, step]).\n",
      "     |      \n",
      "     |      This fixes the problem with randint() which includes the\n",
      "     |      endpoint; in Python this is usually not what you want.\n",
      "     |  \n",
      "     |  sample(self, population, k)\n",
      "     |      Chooses k unique random elements from a population sequence or set.\n",
      "     |      \n",
      "     |      Returns a new list containing elements from the population while\n",
      "     |      leaving the original population unchanged.  The resulting list is\n",
      "     |      in selection order so that all sub-slices will also be valid random\n",
      "     |      samples.  This allows raffle winners (the sample) to be partitioned\n",
      "     |      into grand prize and second place winners (the subslices).\n",
      "     |      \n",
      "     |      Members of the population need not be hashable or unique.  If the\n",
      "     |      population contains repeats, then each occurrence is a possible\n",
      "     |      selection in the sample.\n",
      "     |      \n",
      "     |      To choose a sample in a range of integers, use range as an argument.\n",
      "     |      This is especially fast and space efficient for sampling from a\n",
      "     |      large population:   sample(range(10000000), 60)\n",
      "     |  \n",
      "     |  seed(self, a=None, version=2)\n",
      "     |      Initialize internal state from hashable object.\n",
      "     |      \n",
      "     |      None or no argument seeds from current time or from an operating\n",
      "     |      system specific randomness source if available.\n",
      "     |      \n",
      "     |      If *a* is an int, all bits are used.\n",
      "     |      \n",
      "     |      For version 2 (the default), all of the bits are used if *a* is a str,\n",
      "     |      bytes, or bytearray.  For version 1 (provided for reproducing random\n",
      "     |      sequences from older versions of Python), the algorithm for str and\n",
      "     |      bytes generates a narrower range of seeds.\n",
      "     |  \n",
      "     |  setstate(self, state)\n",
      "     |      Restore internal state from object returned by getstate().\n",
      "     |  \n",
      "     |  shuffle(self, x, random=None)\n",
      "     |      Shuffle list x in place, and return None.\n",
      "     |      \n",
      "     |      Optional argument random is a 0-argument function returning a\n",
      "     |      random float in [0.0, 1.0); if it is the default None, the\n",
      "     |      standard random.random will be used.\n",
      "     |  \n",
      "     |  triangular(self, low=0.0, high=1.0, mode=None)\n",
      "     |      Triangular distribution.\n",
      "     |      \n",
      "     |      Continuous distribution bounded by given lower and upper limits,\n",
      "     |      and having a given mode value in-between.\n",
      "     |      \n",
      "     |      http://en.wikipedia.org/wiki/Triangular_distribution\n",
      "     |  \n",
      "     |  uniform(self, a, b)\n",
      "     |      Get a random number in the range [a, b) or [a, b] depending on rounding.\n",
      "     |  \n",
      "     |  vonmisesvariate(self, mu, kappa)\n",
      "     |      Circular data distribution.\n",
      "     |      \n",
      "     |      mu is the mean angle, expressed in radians between 0 and 2*pi, and\n",
      "     |      kappa is the concentration parameter, which must be greater than or\n",
      "     |      equal to zero.  If kappa is equal to zero, this distribution reduces\n",
      "     |      to a uniform random angle over the range 0 to 2*pi.\n",
      "     |  \n",
      "     |  weibullvariate(self, alpha, beta)\n",
      "     |      Weibull distribution.\n",
      "     |      \n",
      "     |      alpha is the scale parameter and beta is the shape parameter.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Class methods defined here:\n",
      "     |  \n",
      "     |  __init_subclass__(**kwargs) from builtins.type\n",
      "     |      Control how subclasses generate random integers.\n",
      "     |      \n",
      "     |      The algorithm a subclass can use depends on the random() and/or\n",
      "     |      getrandbits() implementation available to it and determines\n",
      "     |      whether it can generate random integers from arbitrarily large\n",
      "     |      ranges.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data descriptors defined here:\n",
      "     |  \n",
      "     |  __dict__\n",
      "     |      dictionary for instance variables (if defined)\n",
      "     |  \n",
      "     |  __weakref__\n",
      "     |      list of weak references to the object (if defined)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data and other attributes defined here:\n",
      "     |  \n",
      "     |  VERSION = 3\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Methods inherited from _random.Random:\n",
      "     |  \n",
      "     |  __getattribute__(self, name, /)\n",
      "     |      Return getattr(self, name).\n",
      "     |  \n",
      "     |  getrandbits(self, k, /)\n",
      "     |      getrandbits(k) -> x.  Generates an int with k random bits.\n",
      "     |  \n",
      "     |  random(self, /)\n",
      "     |      random() -> x in the interval [0, 1).\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Static methods inherited from _random.Random:\n",
      "     |  \n",
      "     |  __new__(*args, **kwargs) from builtins.type\n",
      "     |      Create and return a new object.  See help(type) for accurate signature.\n",
      "    \n",
      "    class SystemRandom(Random)\n",
      "     |  SystemRandom(x=None)\n",
      "     |  \n",
      "     |  Alternate random number generator using sources provided\n",
      "     |  by the operating system (such as /dev/urandom on Unix or\n",
      "     |  CryptGenRandom on Windows).\n",
      "     |  \n",
      "     |   Not available on all systems (see os.urandom() for details).\n",
      "     |  \n",
      "     |  Method resolution order:\n",
      "     |      SystemRandom\n",
      "     |      Random\n",
      "     |      _random.Random\n",
      "     |      builtins.object\n",
      "     |  \n",
      "     |  Methods defined here:\n",
      "     |  \n",
      "     |  getrandbits(self, k)\n",
      "     |      getrandbits(k) -> x.  Generates an int with k random bits.\n",
      "     |  \n",
      "     |  getstate = _notimplemented(self, *args, **kwds)\n",
      "     |  \n",
      "     |  random(self)\n",
      "     |      Get the next random number in the range [0.0, 1.0).\n",
      "     |  \n",
      "     |  seed(self, *args, **kwds)\n",
      "     |      Stub method.  Not used for a system random number generator.\n",
      "     |  \n",
      "     |  setstate = _notimplemented(self, *args, **kwds)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Methods inherited from Random:\n",
      "     |  \n",
      "     |  __getstate__(self)\n",
      "     |      # Issue 17489: Since __reduce__ was defined to fix #759889 this is no\n",
      "     |      # longer called; we leave it here because it has been here since random was\n",
      "     |      # rewritten back in 2001 and why risk breaking something.\n",
      "     |  \n",
      "     |  __init__(self, x=None)\n",
      "     |      Initialize an instance.\n",
      "     |      \n",
      "     |      Optional argument x controls seeding, as for Random.seed().\n",
      "     |  \n",
      "     |  __reduce__(self)\n",
      "     |      Helper for pickle.\n",
      "     |  \n",
      "     |  __setstate__(self, state)\n",
      "     |  \n",
      "     |  betavariate(self, alpha, beta)\n",
      "     |      Beta distribution.\n",
      "     |      \n",
      "     |      Conditions on the parameters are alpha > 0 and beta > 0.\n",
      "     |      Returned values range between 0 and 1.\n",
      "     |  \n",
      "     |  choice(self, seq)\n",
      "     |      Choose a random element from a non-empty sequence.\n",
      "     |  \n",
      "     |  choices(self, population, weights=None, *, cum_weights=None, k=1)\n",
      "     |      Return a k sized list of population elements chosen with replacement.\n",
      "     |      \n",
      "     |      If the relative weights or cumulative weights are not specified,\n",
      "     |      the selections are made with equal probability.\n",
      "     |  \n",
      "     |  expovariate(self, lambd)\n",
      "     |      Exponential distribution.\n",
      "     |      \n",
      "     |      lambd is 1.0 divided by the desired mean.  It should be\n",
      "     |      nonzero.  (The parameter would be called \"lambda\", but that is\n",
      "     |      a reserved word in Python.)  Returned values range from 0 to\n",
      "     |      positive infinity if lambd is positive, and from negative\n",
      "     |      infinity to 0 if lambd is negative.\n",
      "     |  \n",
      "     |  gammavariate(self, alpha, beta)\n",
      "     |      Gamma distribution.  Not the gamma function!\n",
      "     |      \n",
      "     |      Conditions on the parameters are alpha > 0 and beta > 0.\n",
      "     |      \n",
      "     |      The probability distribution function is:\n",
      "     |      \n",
      "     |                  x ** (alpha - 1) * math.exp(-x / beta)\n",
      "     |        pdf(x) =  --------------------------------------\n",
      "     |                    math.gamma(alpha) * beta ** alpha\n",
      "     |  \n",
      "     |  gauss(self, mu, sigma)\n",
      "     |      Gaussian distribution.\n",
      "     |      \n",
      "     |      mu is the mean, and sigma is the standard deviation.  This is\n",
      "     |      slightly faster than the normalvariate() function.\n",
      "     |      \n",
      "     |      Not thread-safe without a lock around calls.\n",
      "     |  \n",
      "     |  lognormvariate(self, mu, sigma)\n",
      "     |      Log normal distribution.\n",
      "     |      \n",
      "     |      If you take the natural logarithm of this distribution, you'll get a\n",
      "     |      normal distribution with mean mu and standard deviation sigma.\n",
      "     |      mu can have any value, and sigma must be greater than zero.\n",
      "     |  \n",
      "     |  normalvariate(self, mu, sigma)\n",
      "     |      Normal distribution.\n",
      "     |      \n",
      "     |      mu is the mean, and sigma is the standard deviation.\n",
      "     |  \n",
      "     |  paretovariate(self, alpha)\n",
      "     |      Pareto distribution.  alpha is the shape parameter.\n",
      "     |  \n",
      "     |  randint(self, a, b)\n",
      "     |      Return random integer in range [a, b], including both end points.\n",
      "     |  \n",
      "     |  randrange(self, start, stop=None, step=1, _int=<class 'int'>)\n",
      "     |      Choose a random item from range(start, stop[, step]).\n",
      "     |      \n",
      "     |      This fixes the problem with randint() which includes the\n",
      "     |      endpoint; in Python this is usually not what you want.\n",
      "     |  \n",
      "     |  sample(self, population, k)\n",
      "     |      Chooses k unique random elements from a population sequence or set.\n",
      "     |      \n",
      "     |      Returns a new list containing elements from the population while\n",
      "     |      leaving the original population unchanged.  The resulting list is\n",
      "     |      in selection order so that all sub-slices will also be valid random\n",
      "     |      samples.  This allows raffle winners (the sample) to be partitioned\n",
      "     |      into grand prize and second place winners (the subslices).\n",
      "     |      \n",
      "     |      Members of the population need not be hashable or unique.  If the\n",
      "     |      population contains repeats, then each occurrence is a possible\n",
      "     |      selection in the sample.\n",
      "     |      \n",
      "     |      To choose a sample in a range of integers, use range as an argument.\n",
      "     |      This is especially fast and space efficient for sampling from a\n",
      "     |      large population:   sample(range(10000000), 60)\n",
      "     |  \n",
      "     |  shuffle(self, x, random=None)\n",
      "     |      Shuffle list x in place, and return None.\n",
      "     |      \n",
      "     |      Optional argument random is a 0-argument function returning a\n",
      "     |      random float in [0.0, 1.0); if it is the default None, the\n",
      "     |      standard random.random will be used.\n",
      "     |  \n",
      "     |  triangular(self, low=0.0, high=1.0, mode=None)\n",
      "     |      Triangular distribution.\n",
      "     |      \n",
      "     |      Continuous distribution bounded by given lower and upper limits,\n",
      "     |      and having a given mode value in-between.\n",
      "     |      \n",
      "     |      http://en.wikipedia.org/wiki/Triangular_distribution\n",
      "     |  \n",
      "     |  uniform(self, a, b)\n",
      "     |      Get a random number in the range [a, b) or [a, b] depending on rounding.\n",
      "     |  \n",
      "     |  vonmisesvariate(self, mu, kappa)\n",
      "     |      Circular data distribution.\n",
      "     |      \n",
      "     |      mu is the mean angle, expressed in radians between 0 and 2*pi, and\n",
      "     |      kappa is the concentration parameter, which must be greater than or\n",
      "     |      equal to zero.  If kappa is equal to zero, this distribution reduces\n",
      "     |      to a uniform random angle over the range 0 to 2*pi.\n",
      "     |  \n",
      "     |  weibullvariate(self, alpha, beta)\n",
      "     |      Weibull distribution.\n",
      "     |      \n",
      "     |      alpha is the scale parameter and beta is the shape parameter.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Class methods inherited from Random:\n",
      "     |  \n",
      "     |  __init_subclass__(**kwargs) from builtins.type\n",
      "     |      Control how subclasses generate random integers.\n",
      "     |      \n",
      "     |      The algorithm a subclass can use depends on the random() and/or\n",
      "     |      getrandbits() implementation available to it and determines\n",
      "     |      whether it can generate random integers from arbitrarily large\n",
      "     |      ranges.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data descriptors inherited from Random:\n",
      "     |  \n",
      "     |  __dict__\n",
      "     |      dictionary for instance variables (if defined)\n",
      "     |  \n",
      "     |  __weakref__\n",
      "     |      list of weak references to the object (if defined)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data and other attributes inherited from Random:\n",
      "     |  \n",
      "     |  VERSION = 3\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Methods inherited from _random.Random:\n",
      "     |  \n",
      "     |  __getattribute__(self, name, /)\n",
      "     |      Return getattr(self, name).\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Static methods inherited from _random.Random:\n",
      "     |  \n",
      "     |  __new__(*args, **kwargs) from builtins.type\n",
      "     |      Create and return a new object.  See help(type) for accurate signature.\n",
      "\n",
      "FUNCTIONS\n",
      "    betavariate(alpha, beta) method of Random instance\n",
      "        Beta distribution.\n",
      "        \n",
      "        Conditions on the parameters are alpha > 0 and beta > 0.\n",
      "        Returned values range between 0 and 1.\n",
      "    \n",
      "    choice(seq) method of Random instance\n",
      "        Choose a random element from a non-empty sequence.\n",
      "    \n",
      "    choices(population, weights=None, *, cum_weights=None, k=1) method of Random instance\n",
      "        Return a k sized list of population elements chosen with replacement.\n",
      "        \n",
      "        If the relative weights or cumulative weights are not specified,\n",
      "        the selections are made with equal probability.\n",
      "    \n",
      "    expovariate(lambd) method of Random instance\n",
      "        Exponential distribution.\n",
      "        \n",
      "        lambd is 1.0 divided by the desired mean.  It should be\n",
      "        nonzero.  (The parameter would be called \"lambda\", but that is\n",
      "        a reserved word in Python.)  Returned values range from 0 to\n",
      "        positive infinity if lambd is positive, and from negative\n",
      "        infinity to 0 if lambd is negative.\n",
      "    \n",
      "    gammavariate(alpha, beta) method of Random instance\n",
      "        Gamma distribution.  Not the gamma function!\n",
      "        \n",
      "        Conditions on the parameters are alpha > 0 and beta > 0.\n",
      "        \n",
      "        The probability distribution function is:\n",
      "        \n",
      "                    x ** (alpha - 1) * math.exp(-x / beta)\n",
      "          pdf(x) =  --------------------------------------\n",
      "                      math.gamma(alpha) * beta ** alpha\n",
      "    \n",
      "    gauss(mu, sigma) method of Random instance\n",
      "        Gaussian distribution.\n",
      "        \n",
      "        mu is the mean, and sigma is the standard deviation.  This is\n",
      "        slightly faster than the normalvariate() function.\n",
      "        \n",
      "        Not thread-safe without a lock around calls.\n",
      "    \n",
      "    getrandbits(k, /) method of Random instance\n",
      "        getrandbits(k) -> x.  Generates an int with k random bits.\n",
      "    \n",
      "    getstate() method of Random instance\n",
      "        Return internal state; can be passed to setstate() later.\n",
      "    \n",
      "    lognormvariate(mu, sigma) method of Random instance\n",
      "        Log normal distribution.\n",
      "        \n",
      "        If you take the natural logarithm of this distribution, you'll get a\n",
      "        normal distribution with mean mu and standard deviation sigma.\n",
      "        mu can have any value, and sigma must be greater than zero.\n",
      "    \n",
      "    normalvariate(mu, sigma) method of Random instance\n",
      "        Normal distribution.\n",
      "        \n",
      "        mu is the mean, and sigma is the standard deviation.\n",
      "    \n",
      "    paretovariate(alpha) method of Random instance\n",
      "        Pareto distribution.  alpha is the shape parameter.\n",
      "    \n",
      "    randint(a, b) method of Random instance\n",
      "        Return random integer in range [a, b], including both end points.\n",
      "    \n",
      "    random() method of Random instance\n",
      "        random() -> x in the interval [0, 1).\n",
      "    \n",
      "    randrange(start, stop=None, step=1, _int=<class 'int'>) method of Random instance\n",
      "        Choose a random item from range(start, stop[, step]).\n",
      "        \n",
      "        This fixes the problem with randint() which includes the\n",
      "        endpoint; in Python this is usually not what you want.\n",
      "    \n",
      "    sample(population, k) method of Random instance\n",
      "        Chooses k unique random elements from a population sequence or set.\n",
      "        \n",
      "        Returns a new list containing elements from the population while\n",
      "        leaving the original population unchanged.  The resulting list is\n",
      "        in selection order so that all sub-slices will also be valid random\n",
      "        samples.  This allows raffle winners (the sample) to be partitioned\n",
      "        into grand prize and second place winners (the subslices).\n",
      "        \n",
      "        Members of the population need not be hashable or unique.  If the\n",
      "        population contains repeats, then each occurrence is a possible\n",
      "        selection in the sample.\n",
      "        \n",
      "        To choose a sample in a range of integers, use range as an argument.\n",
      "        This is especially fast and space efficient for sampling from a\n",
      "        large population:   sample(range(10000000), 60)\n",
      "    \n",
      "    seed(a=None, version=2) method of Random instance\n",
      "        Initialize internal state from hashable object.\n",
      "        \n",
      "        None or no argument seeds from current time or from an operating\n",
      "        system specific randomness source if available.\n",
      "        \n",
      "        If *a* is an int, all bits are used.\n",
      "        \n",
      "        For version 2 (the default), all of the bits are used if *a* is a str,\n",
      "        bytes, or bytearray.  For version 1 (provided for reproducing random\n",
      "        sequences from older versions of Python), the algorithm for str and\n",
      "        bytes generates a narrower range of seeds.\n",
      "    \n",
      "    setstate(state) method of Random instance\n",
      "        Restore internal state from object returned by getstate().\n",
      "    \n",
      "    shuffle(x, random=None) method of Random instance\n",
      "        Shuffle list x in place, and return None.\n",
      "        \n",
      "        Optional argument random is a 0-argument function returning a\n",
      "        random float in [0.0, 1.0); if it is the default None, the\n",
      "        standard random.random will be used.\n",
      "    \n",
      "    triangular(low=0.0, high=1.0, mode=None) method of Random instance\n",
      "        Triangular distribution.\n",
      "        \n",
      "        Continuous distribution bounded by given lower and upper limits,\n",
      "        and having a given mode value in-between.\n",
      "        \n",
      "        http://en.wikipedia.org/wiki/Triangular_distribution\n",
      "    \n",
      "    uniform(a, b) method of Random instance\n",
      "        Get a random number in the range [a, b) or [a, b] depending on rounding.\n",
      "    \n",
      "    vonmisesvariate(mu, kappa) method of Random instance\n",
      "        Circular data distribution.\n",
      "        \n",
      "        mu is the mean angle, expressed in radians between 0 and 2*pi, and\n",
      "        kappa is the concentration parameter, which must be greater than or\n",
      "        equal to zero.  If kappa is equal to zero, this distribution reduces\n",
      "        to a uniform random angle over the range 0 to 2*pi.\n",
      "    \n",
      "    weibullvariate(alpha, beta) method of Random instance\n",
      "        Weibull distribution.\n",
      "        \n",
      "        alpha is the scale parameter and beta is the shape parameter.\n",
      "\n",
      "DATA\n",
      "    __all__ = ['Random', 'seed', 'random', 'uniform', 'randint', 'choice',...\n",
      "\n",
      "FILE\n",
      "    c:\\programdata\\anaconda3\\lib\\random.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(random)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0cd59642",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时间节点的秒数： 32\n",
      "Not an odd second.\n",
      "等待的时间： 2\n",
      "————————————————\n",
      "当前时间节点的秒数： 34\n",
      "Not an odd second.\n",
      "等待的时间： 4\n",
      "————————————————\n",
      "当前时间节点的秒数： 38\n",
      "Not an odd second.\n",
      "等待的时间： 3\n",
      "————————————————\n",
      "当前时间节点的秒数： 41\n",
      "This second seems a little odd.\n",
      "等待的时间： 3\n",
      "————————————————\n",
      "当前时间节点的秒数： 44\n",
      "Not an odd second.\n",
      "等待的时间： 5\n",
      "————————————————\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "for i in range(5):\n",
    "    right_this_time = datetime.today().second\n",
    "    print(\"当前时间节点的秒数：\",right_this_time)\n",
    "    if right_this_time in odds:\n",
    "        print(\"This second seems a little odd.\")    \n",
    "    else:\n",
    "        print(\"Not an odd second.\")\n",
    "    wait_time = random.randint(1,5)\n",
    "    print(\"等待的时间：\",wait_time)\n",
    "    print(\"————————————————\")\n",
    "    # time.sleep暂停取到的秒数后再继续运行程序\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52472883",
   "metadata": {},
   "source": [
    "* 如果没有wait_time和time.sleep()会取到五次同样的运行结果，因为程序的运行时间过快，不到一秒。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9877994a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99 bottles of beer on the wall.\n",
      "99 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "98 bottles of beer on the wall.\n",
      "98 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "97 bottles of beer on the wall.\n",
      "97 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "96 bottles of beer on the wall.\n",
      "96 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "95 bottles of beer on the wall.\n",
      "95 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "94 bottles of beer on the wall.\n",
      "94 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "93 bottles of beer on the wall.\n",
      "93 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "92 bottles of beer on the wall.\n",
      "92 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "91 bottles of beer on the wall.\n",
      "91 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "90 bottles of beer on the wall.\n",
      "90 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "89 bottles of beer on the wall.\n",
      "89 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "88 bottles of beer on the wall.\n",
      "88 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "87 bottles of beer on the wall.\n",
      "87 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "86 bottles of beer on the wall.\n",
      "86 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "85 bottles of beer on the wall.\n",
      "85 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "84 bottles of beer on the wall.\n",
      "84 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "83 bottles of beer on the wall.\n",
      "83 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "82 bottles of beer on the wall.\n",
      "82 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "81 bottles of beer on the wall.\n",
      "81 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "80 bottles of beer on the wall.\n",
      "80 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "79 bottles of beer on the wall.\n",
      "79 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "78 bottles of beer on the wall.\n",
      "78 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "77 bottles of beer on the wall.\n",
      "77 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "76 bottles of beer on the wall.\n",
      "76 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "75 bottles of beer on the wall.\n",
      "75 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "74 bottles of beer on the wall.\n",
      "74 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "73 bottles of beer on the wall.\n",
      "73 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "72 bottles of beer on the wall.\n",
      "72 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "71 bottles of beer on the wall.\n",
      "71 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "70 bottles of beer on the wall.\n",
      "70 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "69 bottles of beer on the wall.\n",
      "69 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "68 bottles of beer on the wall.\n",
      "68 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "67 bottles of beer on the wall.\n",
      "67 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "66 bottles of beer on the wall.\n",
      "66 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "65 bottles of beer on the wall.\n",
      "65 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "64 bottles of beer on the wall.\n",
      "64 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "63 bottles of beer on the wall.\n",
      "63 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "62 bottles of beer on the wall.\n",
      "62 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "61 bottles of beer on the wall.\n",
      "61 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "60 bottles of beer on the wall.\n",
      "60 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "59 bottles of beer on the wall.\n",
      "59 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "58 bottles of beer on the wall.\n",
      "58 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "57 bottles of beer on the wall.\n",
      "57 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "56 bottles of beer on the wall.\n",
      "56 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "55 bottles of beer on the wall.\n",
      "55 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "54 bottles of beer on the wall.\n",
      "54 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "53 bottles of beer on the wall.\n",
      "53 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "52 bottles of beer on the wall.\n",
      "52 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "51 bottles of beer on the wall.\n",
      "51 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "50 bottles of beer on the wall.\n",
      "50 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "49 bottles of beer on the wall.\n",
      "49 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "48 bottles of beer on the wall.\n",
      "48 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "47 bottles of beer on the wall.\n",
      "47 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "46 bottles of beer on the wall.\n",
      "46 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "45 bottles of beer on the wall.\n",
      "45 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "44 bottles of beer on the wall.\n",
      "44 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "43 bottles of beer on the wall.\n",
      "43 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "42 bottles of beer on the wall.\n",
      "42 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "41 bottles of beer on the wall.\n",
      "41 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "40 bottles of beer on the wall.\n",
      "40 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "39 bottles of beer on the wall.\n",
      "39 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "38 bottles of beer on the wall.\n",
      "38 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "37 bottles of beer on the wall.\n",
      "37 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "36 bottles of beer on the wall.\n",
      "36 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "35 bottles of beer on the wall.\n",
      "35 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "34 bottles of beer on the wall.\n",
      "34 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "33 bottles of beer on the wall.\n",
      "33 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "32 bottles of beer on the wall.\n",
      "32 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "31 bottles of beer on the wall.\n",
      "31 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "30 bottles of beer on the wall.\n",
      "30 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "29 bottles of beer on the wall.\n",
      "29 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "28 bottles of beer on the wall.\n",
      "28 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "27 bottles of beer on the wall.\n",
      "27 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "26 bottles of beer on the wall.\n",
      "26 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "25 bottles of beer on the wall.\n",
      "25 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "24 bottles of beer on the wall.\n",
      "24 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "23 bottles of beer on the wall.\n",
      "23 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "22 bottles of beer on the wall.\n",
      "22 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "21 bottles of beer on the wall.\n",
      "21 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "20 bottles of beer on the wall.\n",
      "20 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "19 bottles of beer on the wall.\n",
      "19 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "18 bottles of beer on the wall.\n",
      "18 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "17 bottles of beer on the wall.\n",
      "17 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "16 bottles of beer on the wall.\n",
      "16 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "15 bottles of beer on the wall.\n",
      "15 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "14 bottles of beer on the wall.\n",
      "14 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "13 bottles of beer on the wall.\n",
      "13 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "12 bottles of beer on the wall.\n",
      "12 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "11 bottles of beer on the wall.\n",
      "11 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "10 bottles of beer on the wall.\n",
      "10 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "9 bottles of beer on the wall.\n",
      "9 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "8 bottles of beer on the wall.\n",
      "8 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "7 bottles of beer on the wall.\n",
      "7 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "6 bottles of beer on the wall.\n",
      "6 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "5 bottles of beer on the wall.\n",
      "5 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "4 bottles of beer on the wall.\n",
      "4 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "3 bottles of beer on the wall.\n",
      "3 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "\n",
      "2 bottles of beer on the wall.\n",
      "2 bottles of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "1 bottle of beer on the wall.\n",
      "\n",
      "1 bottle of beer on the wall.\n",
      "1 bottle of beer.\n",
      "Take one down.\n",
      "Past it around.\n",
      "No more bottles of beer on the wall.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "word = \"bottles\"\n",
    "for beer_num in range(99,0,-1):\n",
    "    print(beer_num,word,\"of beer on the wall.\")\n",
    "    print(beer_num,word,\"of beer.\")\n",
    "    print(\"Take one down.\")\n",
    "    print(\"Past it around.\")\n",
    "    if beer_num == 1:\n",
    "        print(\"No more bottles of beer on the wall.\")\n",
    "    else:\n",
    "        new_num = beer_num - 1\n",
    "        if new_num == 1:\n",
    "            word = \"bottle\"\n",
    "            print(new_num,word,\"of beer on the wall.\")\n",
    "    print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
